import { http, HttpResponse, } from 'msw';
import { menuList, managerMenuList, userMenuList } from '@/mocks/menu';
import { store } from '@/store';
import { Faker, zh_CN } from '@faker-js/faker'
const faker = new Faker({ locale: [zh_CN] })
// 登录接口处理器
const loginHandler = http.post('/api/login', async ({ request }: { request: Request }) => {
  const { username, password } = await request.json();

  if (username === "admin" && password === "1111") {
    return HttpResponse.json({
      code: 200,
      message: "登录成功",
      data: {
        username: "赵铁柱",
        token: "mocktoken123456admin",
        btnAuth: ["add", "edit", "delete"]
      }
    });
  } else if (username === "manager" && password === "manager123123") {
    return HttpResponse.json({
      code: 200,
      message: "登录成功",
      data: {
        username: "manager",
        token: "mocktoken123456manager",
        btnAuth: ["add", "edit"]
      }
    });
  } else if (username === "user" && password === "user123123") {
    return HttpResponse.json({
      code: 200,
      message: "登录成功",
      data: {
        username: "user",
        token: "mocktoken123456user",
        btnAuth: ["add"]
      }
    });
  } else {
    return HttpResponse.json({
      code: 401,
      message: "用户名或密码有误",
      data: ""
    }, { status: 401 });
  }
});

// 菜单项接口处理器
const menuHandler = http.get('/api/menu', async () => {
  const token = store.getState().auth?.token;
  if (token === "mocktoken123456admin") {
    return HttpResponse.json({
      code: 200,
      message: "请求成功",
      data: menuList
    });
  } else if (token === "mocktoken123456user") {
    return HttpResponse.json({
      code: 200,
      message: "请求成功",
      data: userMenuList
    });
  } else if (token === "mocktoken123456manager") {
    return HttpResponse.json({
      code: 200,
      message: "请求成功",
      data: managerMenuList
    });
  } else {
    return HttpResponse.json({
      code: 401,
      message: "失败",
      data: []
    }, { status: 401 });
  }
});

// dash中的图表接口
const energyHander = http.get('/api/energyData', async () => {
  return HttpResponse.json({
    code: 200,
    message: "请求成功",
    data:
      [
        {
          name: '煤',
          data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
          name: '气',
          data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
          name: '油',
          data: [150, 232, 201, 154, 190, 330, 410]
        },
        {
          name: '电',
          data: [320, 332, 301, 334, 390, 330, 320]
        },
        {
          name: '热',
          data: [820, 932, 901, 934, 1290, 1330, 1320]
        }
      ]
  })
})

// 登出接口处理器
const logoutHandler = http.get('/api/logout', async () => {
  return HttpResponse.json({
    code: 200,
    message: "登出成功",
    data: ""
  });
});

//住户列表的接口
const userLisetHandler = http.post('/api/userList', async ({ request }: { request: Request }) => {
  const res = JSON.parse(await request.json());
  console.log(res)
  const INDUSTRIES = ['制造业', '服务行业', '新媒体', '美业', '新能源', '物流', '电商']
  const list = Array.from({ length: 70 }, () => ({
    id: faker.string.numeric(6), // 6位数字字符串
    name: faker.person.fullName(), // 中文姓名
    tel: faker.phone.number(), // 默认会根据 locale 生成中国手机号（如 138****1234）
    business: faker.helpers.arrayElement(INDUSTRIES),
    email: faker.internet.email(),
    creditCode: faker.string.numeric(18), // 统一社会信用代码（模拟）
    integratedCode: faker.string.numeric(18), // 模拟
    organizationCode: faker.string.numeric(18), // 模拟
    legalPersonName: faker.person.fullName(), // 法人姓名
  }))
  return HttpResponse.json({
    code: 200,
    message: "请求成功",
    data: { list }
  })
})


export const handlers = [
  loginHandler,
  menuHandler,
  logoutHandler,
  energyHander,
  userLisetHandler
];